package CH02链表结构.LeetCode链表;

public class LC82 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode left = head;
        ListNode right = left.next;
        boolean flag = false;
        while (right != null) {
            if (left.val == right.val) {
                flag = true;
                left.next = null;
                ListNode temp = right;
                right = right.next;
                temp.next = null;
            } else {
                if (flag) {
                    pre.next = right;
                    left = right;
                    right = left.next;
                    flag = false;
                } else {
                    pre = left;
                    left = right;
                    right = left.next;
                }
            }
        }
        if (flag) pre.next = null;
        return dummy.next;
    }
}